<?php

defined('_SECURITY_TOKEN') or die("caia fora");

class controllerextraction //extends AppController
{

	function __construct(  )
	{
		
		$task	=	application::getTask();//( empty($_REQUEST['task'])? '':$_REQUEST['task']);
		
		switch( $task ){
			
			case 'execute':
				
				
				try {
        						
					//exige autenticacao de usuário
					application::IsAuthenticationRequired(true);
					
								
					$parameters =	application::getParameters();
					
								
					$result 	=	$this->viewExecute(application::getUser_id(),
																$parameters);
					
					if( $result ){
						
						$result=array("status"=>1,
									"data"=>MSG_28_VIEW_EXECUTADA_COM_SUCESSO,//application::arrayToJson($result),
									"notification"=>28);
					}else{
						
						$result=array("status"=>0,
									"data"=>MSG_17_ERRO_AO_CONECTAR_SGDB,
									"notification"=>17);
					}
					

	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );

			    }

				
				
				break;
			case 'data':
				
				
				try {
        						
					//exige autenticacao de usuário
					application::IsAuthenticationRequired(true);
					
								
					$parameters =	application::getParameters();
								
					$result 	=	$this->getViewDataDb(
													application::getUser_id(),
													$parameters);
					
					if( $result ){
						
						$result=array("status"=>1,
									"data"=>application::arrayToJson($result),
									"notification"=>-1);
					}else{
						
						$result=array("status"=>0,
						"data"=>MSG_24_TALVEZ_SEJA_NECESSARIO_EXECUTAR_EXTRACAO,
									"notification"=>25);
					}
					
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    }

				
				
				break;
			
			default:
				
				//echo "view não definida.";
				throw new AppException( MSG_23_METODO_NAO_DEFINIDO );
							
				break;
		}
		
	}
	
	
	
	public function getViewDataDb( $user_id, $parameters ){
		
		$result=false;
				
		try {
			
			$model	=	application::getModel('extraction');
	
			$result	=	$model->getViewDataDb( $user_id, $parameters );
		
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	
		return $result;
		
	}

	


	public function viewExecute( $user_id, $parameters ){
				
		$result=false;
		
		try {
			
			$model	=	application::getModel('extraction');
	
			$result	=	$model->viewExecute( $user_id, $parameters );
		
		}
	    catch (AppException $e) {
			throw new AppException( $e->getMessage() );
	    	
	    }

		return $result;
		
	}
	
	
	

	
	
}